//
//  CityListViewController.m
//  DMEducation
//
//  Created by uplooking on 2018/7/31.
//  Copyright © 2018年 CRLong. All rights reserved.
//

#import "CityListViewController.h"

@interface CityListViewController ()
<
UITableViewDelegate,
UITableViewDataSource,
DZNEmptyDataSetSource,
DZNEmptyDataSetDelegate
>
{
    
    UITableView * tableView;
    NSMutableArray * arrayResult;
    NSString * address;
  
}

@end

@implementation CityListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"城市列表";
    [self setBackLeftBarButtonItemAction:@selector(backAction:)];
    
    arrayResult = [NSMutableArray array];
    [self initView];
    [self startSKV];
    [self getCityAreaListData];
}

- (void)backAction:(UIButton *)sender{
    
    [self.navigationController popViewControllerAnimated:YES];
    
}

- (void)initView{
    
    tableView = [[UITableView alloc] init];
    [tableView setBackgroundColor:[UIColor clearColor]];
    [tableView setDelegate:self];
    [tableView setDataSource:self];
    [tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [tableView setShowsHorizontalScrollIndicator:NO];
    [tableView setShowsVerticalScrollIndicator:NO];
    [tableView setEmptyDataSetDelegate:self];
    [tableView setEmptyDataSetSource:self];
    [self.view addSubview:tableView];
    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
}

#pragma mark--tableView代理
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
    NSInteger count = [arrayResult count];
    return count;
}

- (UITableViewCell *)tableView:(UITableView *)_tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *identifier = @"CityListTextCell";
    
    CityListTextCell * cell = [_tableView dequeueReusableCellWithIdentifier:identifier];
    
    if (cell == nil) {
        
        cell = [[CityListTextCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    
    CityIndexModel * model = arrayResult[indexPath.row];
 
    [cell setTitle:model.name];
    
    
    return cell;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 50 * kAutoScanleX;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    CityIndexModel * model = arrayResult[indexPath.row];
    
    if (address.length == 0) {
        
        address = model.name;
    }else{
        
        address = [NSString stringWithFormat:@"%@%@",address,model.name];
    }

    
    if (model.child.count != 0) {
        
        [arrayResult removeAllObjects];
        [arrayResult addObjectsFromArray:model.child];
        [tableView reloadData];
        
    }else{
        
        [self backAction:nil];
        
        if (self.blcokFinish) {
            
            self.blcokFinish(model, address);
        }
        
    }
    
}

#pragma mark -- 请求数据
- (void)getCityAreaListData{
    
    WS(weakSelf);
    
    [HttpRequestHelper requestApiAreaBlock:^(NSString *msg, NSArray *array) {
       
        [weakSelf stopSKV];
        [weakSelf hideHud];
   
        if (REQUEST_SUCCESS) {
            
            [arrayResult removeAllObjects];
            [arrayResult addObjectsFromArray:array];
            
        }else{
            
            [self showHint:msg];
        }
        
        [tableView reloadData];
    }];

}


@end
